package com.hengfoxwolf.jetpackdemo
import android.os.Bundle
import android.view.View
import androidx.navigation.NavController
import androidx.navigation.NavOptions
import androidx.navigation.findNavController
import com.hengfoxwolf.jetpackdemo.base.BaseActivity
import com.hengfoxwolf.jetpackdemo.constants.StringConstant
import com.hengfoxwolf.jetpackdemo.databinding.ActivityMainBinding
import com.hengfoxwolf.jetpackdemo.datastore.SharedPreferenceUtil

class MainActivity : BaseActivity<ActivityMainBinding>(ActivityMainBinding::inflate), View.OnClickListener {

    private lateinit var navController: NavController

    private var currentId = HOME
    private var lastNavId = R.id.navigation_home

    override fun init(savedInstanceState: Bundle?) {
        window.navigationBarColor = resources.getColor(R.color.color_eeeeee)

        navController = findNavController(R.id.fragment_container_view)

        mBinding.llHome.setOnClickListener(this)
        mBinding.llDashboard.setOnClickListener(this)
        mBinding.llNotifications.setOnClickListener(this)

        currentId = SharedPreferenceUtil.findValue(StringConstant.TAB_INDEX, HOME)
        exeAction(currentId)
    }

    override fun onClick(view: View?) {
        when (view?.id) {
            R.id.ll_home -> {
                currentId = HOME
            }
            R.id.ll_dashboard -> {
                currentId = DASHBOARD
            }
            R.id.ll_notifications -> {
                currentId = NOTIFICATIONS
            }
        }
        exeAction(currentId)
    }

    override fun onBackPressed() {
        super.onBackPressed()
//        val label = navController.currentDestination?.label
//        val condition = resources.getString(R.string.title_loading) == label
//        if (condition) {
//            currentId = HOME
//            exeAction(currentId)
//        } else {
//            super.onBackPressed()
//        }
    }

    private fun exeAction(id: Int) {
        setTabBackground(id)
        val navId = convert2NavId(id)
        goDestination(navId)
        lastNavId = navId
        SharedPreferenceUtil.putValue(StringConstant.TAB_INDEX, id)
    }

    private fun goDestination(destinationId: Int) {
        navController.navigate(
            destinationId,
            null,
            NavOptions.Builder().setPopUpTo(lastNavId, true).build()
        )
    }

    private fun setTabBackground(currentId: Int) {
        when (currentId) {
            HOME -> {
                mBinding.tvHome.setTextColor(resources.getColor(R.color.purple_200))
                mBinding.tvDashboard.setTextColor(resources.getColor(R.color.purple_700))
                mBinding.tvNotifications.setTextColor(resources.getColor(R.color.purple_700))
            }
            DASHBOARD -> {
                mBinding.tvHome.setTextColor(resources.getColor(R.color.purple_700))
                mBinding.tvDashboard.setTextColor(resources.getColor(R.color.purple_200))
                mBinding.tvNotifications.setTextColor(resources.getColor(R.color.purple_700))
            }
            NOTIFICATIONS -> {
                mBinding.tvHome.setTextColor(resources.getColor(R.color.purple_700))
                mBinding.tvDashboard.setTextColor(resources.getColor(R.color.purple_700))
                mBinding.tvNotifications.setTextColor(resources.getColor(R.color.purple_200))
            }
        }
    }

    private fun convert2NavId(currentId: Int): Int {
        return when (currentId) {
            HOME -> {
                R.id.navigation_home
            }
            DASHBOARD -> {
                R.id.navigation_dashboard
            }
            NOTIFICATIONS -> {
                R.id.navigation_notifications
            }
            else -> {
                R.id.navigation_home
            }
        }
    }

    companion object {
        const val TAG = "MainActivity"

        const val HOME = 0x00
        const val DASHBOARD = 0x01
        const val NOTIFICATIONS = 0x02
    }

}